package net.huashitong.scheduling.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;

import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.scheduling.entity.CapitalSchedules;
import net.huashitong.scheduling.entity.ExpenditureSchedule;
import net.huashitong.scheduling.entity.FinanceRevenue;
import net.huashitong.scheduling.entity.FinancialStatus;
import net.huashitong.scheduling.filter.IndicatorSchedulingFilter;
import net.huashitong.scheduling.service.CapitalSchedulesService;
import net.huashitong.scheduling.service.ExpenditureScheduleService;
import net.huashitong.scheduling.service.FinanceRevenueService;
import net.huashitong.scheduling.service.FinancialStatusService;

@Controller
@RequestMapping("/app/financial/")
public class FinancialController extends JsonBaseController {
	@Autowired
	private FinanceRevenueService financeRevenueService;
	@Autowired
	private FinancialStatusService  financialStatusService;
	@Autowired
	private ExpenditureScheduleService expenditureScheduleService;
	@Autowired
	private  CapitalSchedulesService capitalSchedulesService;
	
	public static final String[] months = {"12","11","10","09","08","07","06","05","04","03","02","01"};
	public static final String[] years={"2022", "2021", "2020","2019","2018","2017"};
	@Override
	protected String getView(String arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	@ResponseBody
	@RequestMapping("/viewData")
	public Message queryFinancial(@RequestBody IndicatorBreakdownFilter filter){
		 List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		//市、县（区）财政收支考核情况
		 Map<String, Object> map = new HashMap<String, Object>();
		 List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		 List<FinanceRevenue> fList=new ArrayList<FinanceRevenue>();
		 List<FinancialStatus> fsList=new ArrayList<FinancialStatus>();
		 List<CapitalSchedules> csList=new ArrayList<CapitalSchedules>();
		 List<ExpenditureSchedule> esList=new ArrayList<ExpenditureSchedule>();
			String time="";
			String times="";
		int ss=0;
	  if(ObjectUtils.isBlank(filter.getDate())){
		 for(int j=0;j<RevenueController.years.length;j++){
			 if(ss==1){
				 break;
			 }
		  	   for(int i=0; i<RevenueController.months.length;i++){
		  		 fList=financeRevenueService.queryByDate(years[j]+months[i]);
		  		 fsList=financialStatusService.queryByDate(months[i],years[j]);
		  		 csList=capitalSchedulesService.queryByDate(months[i],years[j]);
		  		 esList =expenditureScheduleService.queryByDate(months[i],years[j]);
		  		 if(ObjectUtils.isNotBlank(fList)||ObjectUtils.isNotBlank(fsList)||ObjectUtils.isNotBlank(esList)||ObjectUtils.isNotBlank(csList)){
		  			time=years[j]+"年"+months[i]+"月";
		  			times=years[j]+months[i];
		  			ss=1;
		  			break;
		  		 }
		  	   }
		 }
	  }else{
		  String month=filter.getDate().substring(4,6);
  		  String year=filter.getDate().substring(0,4);
  		  fList=financeRevenueService.queryByDate(filter.getDate());
  		  fsList=financialStatusService.queryByDate(month,year);
  		 csList=capitalSchedulesService.queryByDate(month,year);
  		 esList =expenditureScheduleService.queryByDate(month,year);
	  }
	   if(ObjectUtils.isNotBlank(fList)){
		  for(FinanceRevenue fr:fList){
			 Map<String, Object> map1 = new HashMap<String, Object>();
			 map1.put("industryName", fr.getIndustryName());
			 map1.put("predictName", fr.getPredictName());
			 map1.put("predictValue", fr.getPredictValue());
			 map1.put("accumulativeName", fr.getAccumulativeName());
			 map1.put("accumulativeValue", fr.getAccumulativeValue());
			 map1.put("schedule", fr.getSchedule());
			 map1.put("scheduleName", fr.getScheduleName());
			 map1.put("ranking", fr.getRanking());
			 map1.put("unit", fr.getUnit());
			 list.add(map1);
		  }
		 map.put("data",list);
		 map.put("title","全省市、县（区）财政收支考核情况");
		 map.put("type","1");
		 lists.add(map);
		 }
		 
		//财政财量资金
		 Map<String, Object> map2 = new HashMap<String, Object>();
		 List<Map<String, Object>> list2 = new ArrayList<Map<String,Object>>();
		 if(ObjectUtils.isNotBlank(fsList)){
		 for(FinancialStatus fr:fsList){
			 Map<String, Object> mapp = new HashMap<String, Object>();
			 if("0".equals(fr.getState())&&"0".equals(fr.getParentId())){
				 mapp.put("section", fr.getSection());
				 mapp.put("balance", fr.getBalance());
				 list2.add(mapp);
			 }
			 
		 }
		 map2.put("unit", fsList.get(0).getUnit());
		 map2.put("state", "0");
		 map2.put("data", list2);
		 map2.put("name", "财政存量资金前20家单位资金情况");
		 //map2.put("title","财政存量资金(总余额：876.80万元)");
		 map2.put("title","财政存量资金");
		 map2.put("type","2");
		 //当年未使用资金量
		 Map<String, Object> map3 = new HashMap<String, Object>();
		 List<Map<String, Object>> list3= new ArrayList<Map<String,Object>>();
		  for(FinancialStatus fr:fsList){
			 Map<String, Object> mapp = new HashMap<String, Object>();
			 if("1".equals(fr.getState())&&"0".equals(fr.getParentId())){
				 mapp.put("section", fr.getSection());
				 mapp.put("balance", fr.getBalance());
				 list3.add(mapp);
			 }
			 
		 }
		 
		 map3.put("unit", fsList.get(0).getUnit());
		 map3.put("state", "1");
		 map3.put("data", list3);
		 map3.put("name", "当年未使用资金量前20家单位资金情况");
		// map3.put("title","当年未使用资金量(总余额：24,147.93 万元)");
		 map3.put("title", "当年未使用资金量");
		 map3.put("type","2");
		 
		 
		
	    lists.add(map2);
	    lists.add(map3);
	    
	   
		}
		//404永泰县当年财政支出进度排名
		 if(ObjectUtils.isNotBlank(esList)){
			 Map<String, Object> map4 = new HashMap<String, Object>();
			 List<Map<String, Object>> list4=new ArrayList<Map<String,Object>>();
			 int s2=1;
			 for(ExpenditureSchedule es:esList){
				 Map<String, Object> mapp = new HashMap<String, Object>();
				 mapp.put("name", es.getName());
				 mapp.put("yearlyPredictExpenditure", es.getYearlyPredictExpenditure());
				 mapp.put("completeExpenditure", es.getCompleteExpenditure());
				 mapp.put("completeExpenditureProportion", es.getCompleteExpenditureProportion());
				 mapp.put("ranking", es.getRanking());
				 mapp.put("unit", es.getUnit());
				 list4.add(mapp);
				 s2++;
				 if(s2==21){
						break;
				 }
			 }
			 map4.put("unit", esList.get(0).getUnit());
			 map4.put("name", "当年财政支出前20家单位");
			 map4.put("title","当年财政支出");
			 map4.put("data", list4);
			 map4.put("type","3");
			 lists.add(map4); 
		 }
		//405永泰县2018年财政存量资金清理盘活进度排名
		 if(ObjectUtils.isNotBlank(csList)){
			 Map<String, Object> map5 = new HashMap<String, Object>();
			 List<Map<String, Object>> list5=new ArrayList<Map<String,Object>>();
			 int s3=1;
			 for(CapitalSchedules cs:csList){
				 Map<String, Object> mapp = new HashMap<String, Object>();
				 mapp.put("name", cs.getName());
				 mapp.put("total", cs.getTotal());
				 mapp.put("money", cs.getMoney());
				 mapp.put("proportion",cs.getProportion());
				 mapp.put("ranking", cs.getRanking());
				 mapp.put("unit", cs.getUnit());
				 list5.add(mapp);
				 s3++;
				 if(s3==21){
					break;
				 }
			 }
			 map5.put("unit", csList.get(0).getUnit());
			 map5.put("title","财政存量资金清理盘活");
			 map5.put("name", "财政存量资金清理盘活前20家单位");
			 map5.put("data", list5);
			 map5.put("type","4");
			 lists.add(map5);
		 }
		//最后结构
		Map<String, Object> maps = new HashMap<String, Object>();
	    maps.put("title", "财政主要考核指标");
	    if(ObjectUtils.isBlank(filter.getDate())){
			maps.put("date", time);
			maps.put("time", times);
		}else{
			 String month=filter.getDate().substring(4,6);
			 String year=filter.getDate().substring(0,4);
			 time=year+"年"+month+"月";
			maps.put("date", time);
			maps.put("time", filter.getDate());
		}
		maps.put("lists", lists);
		return coverMessage("200","数据获取成功",maps);
	}
	@ResponseBody
	@RequestMapping("/queryViewDataDetail")
	public Message queryViewDataDetail(@RequestBody IndicatorBreakdownFilter filter){
		Map<String, Object> maps = new HashMap<String, Object>();
		List<FinanceRevenue>  frList=financeRevenueService.queryByindustryName(filter);
		if(frList.size()>0){
			maps.put("industryName",filter.getIndustryName());
			maps.put("predictName", frList.get(0).getPredictName());
			maps.put("accumulativeName", frList.get(0).getAccumulativeName());
			maps.put("scheduleName", frList.get(0).getScheduleName());
			maps.put("unit", frList.get(0).getUnit());
			maps.put("lists",frList);
			
		}
		
		return coverMessage("200","数据获取成功",maps);
	}
	
	
	@ResponseBody
	@RequestMapping("/querySectionList")
	public Message queryFinancialDetail(@RequestBody IndicatorSchedulingFilter filter){
		 List<Map<String, Object>> list= new ArrayList<Map<String,Object>>();
		 List<FinancialStatus> fsList=new ArrayList<FinancialStatus>();
		 List<FinancialStatus> fsList1=financialStatusService.query();
		 for(FinancialStatus fs:fsList1){
			 if("12".equals(fs.getMonth())){
				 fsList.add(fs);
			 }
		 }
		 for(FinancialStatus fr:fsList){
			 Map<String, Object> mapp = new HashMap<String, Object>();
			 if(filter.getState().equals(fr.getState())&&"0".equals(fr.getParentId())){
				 mapp.put("sysId", fr.getSysId());
				 mapp.put("section", fr.getSection());
				 list.add(mapp);
			 }
			 
		 }
		return coverMessage("200","数据获取成功",list);
	}
	@ResponseBody
	@RequestMapping("/querySectionDetailList")
	public Message querySectionDetailList(@RequestBody IndicatorSchedulingFilter filter){
		 List<Map<String, Object>> list= new ArrayList<Map<String,Object>>();
		 List<FinancialStatus> fsList=new ArrayList<FinancialStatus>();
		 List<FinancialStatus> fsList1=financialStatusService.query();
		 for(FinancialStatus fs:fsList1){
			 if("12".equals(fs.getMonth())){
				 fsList.add(fs);
			 }
		 }
		 
		 for(FinancialStatus fr:fsList){
			 Map<String, Object> mapp = new HashMap<String, Object>();
			 if(filter.getSysId().equals(fr.getParentId())){
				 mapp.put("name", fr.getName());
				 mapp.put("balance", fr.getBalance());
				 mapp.put("reference", fr.getReference());
				 mapp.put("unit",fr.getUnit());
				 if("1".equals(fr.getState())){
					 mapp.put("date", fr.getDate());
				 }else{
					 mapp.put("date","-");
				 }
				 mapp.put("source", fr.getSource());
				 list.add(mapp);
			 }
			 
		 }
		 return coverMessage("200","数据获取成功",list);
	}
	/**
	 * 404永泰县当年财政支出进度排名详细
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryExpenditureScheduleDetail")
	public Message  queryExpenditureScheduleDetail(@RequestBody IndicatorBreakdownFilter filter){
		List<Map<String, Object>> list= new ArrayList<Map<String,Object>>();
		 String month=filter.getDate().substring(4,6);
 		  String year=filter.getDate().substring(0,4);
 		 List<ExpenditureSchedule>  esList =expenditureScheduleService.queryByDate(month,year);
 		 if(ObjectUtils.isNotBlank(esList)){
 			 for(ExpenditureSchedule es:esList){
				 Map<String, Object> mapp = new HashMap<String, Object>();
				 mapp.put("name", es.getName());
				 mapp.put("yearlyPredictExpenditure", es.getYearlyPredictExpenditure());
				 mapp.put("completeExpenditure", es.getCompleteExpenditure());
				 mapp.put("completeExpenditureProportion", es.getCompleteExpenditureProportion());
				 mapp.put("ranking", es.getRanking());
				 mapp.put("unit", es.getUnit());
				 list.add(mapp);
				}
 		 }
		 return coverMessage("200","数据获取成功",list);
	}
	/**
	 * 405永泰县2018年财政存量资金清理盘活进度排名详细
	 * @param filter
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/queryCapitalSchedulesDetail")
	public Message queryCapitalSchedulesDetail(@RequestBody IndicatorBreakdownFilter filter){
		List<Map<String, Object>> list= new ArrayList<Map<String,Object>>();
		 String month=filter.getDate().substring(4,6);
		  String year=filter.getDate().substring(0,4);
		  List<CapitalSchedules> csList=capitalSchedulesService.queryByDate(month,year);
		  if(ObjectUtils.isNotBlank(csList)){
			  for(CapitalSchedules cs:csList){
					 Map<String, Object> mapp = new HashMap<String, Object>();
					 mapp.put("name", cs.getName());
					 mapp.put("total", cs.getTotal());
					 mapp.put("money", cs.getMoney());
					 mapp.put("proportion",cs.getProportion());
					 mapp.put("ranking", cs.getRanking());
					 mapp.put("unit", cs.getUnit());
					 list.add(mapp);
			  }				 
		  }

		 return coverMessage("200","数据获取成功",list);
	}
}
